<?php

pm_Bootstrap::init();
//pm_Bootstrap::getDbAdapter()->query("SELECT param,val FROM misc WHERE param='clamnotify'");
//$user ="admin";
//$pass = exec('cat /etc/psa/.psa.shadow');
//$dbname="psa";
//$table="clamav_logs";
//$hostname ="localhost";

$Logdirectory = '/usr/local/psa/admin/htdocs/modules/clamav/logs/';
$searchfor = "--- SCAN SUMMARY ---";
$virusstr = "FOUND";

$logfiles = glob($Logdirectory  . "*.txt");
$status =0;
$virus =0;
//$toemail = $options['a'];
$my_mail = "notify@".gethostname();
$my_name = "Virus Scanner at server ".gethostname();
$my_replyto = "donotreply@".gethostname();
$my_subject= "Virus found on ".gethostname()." action needed";
$my_message = "Please review the attached scan results a virus was found";
if(is_array($logfiles)){
 //   echo "files found";
 //   $conn = mysql_connect($hostname,$user, $pass) or die ("Unable to connect to database! Please try again later.");
 //    echo "DB connected";
    foreach($logfiles as $logfile)  {
	   $file = file_get_contents($logfile);
       $file = mysql_real_escape_string($file);
	   if(strpos($file, $searchfor)) { $status = true; }
	   if(strpos($file, $virusstr )) { $virus = true;  }
       pm_Bootstrap::getDbAdapter()->query("INSERT INTO clamav_logs(id, date, results, virus, status) VALUES ( '". basename($logfile)."','". date("Y-m-d H:i:s")."' ,'" .$file."',". $virus." , ".$status.") ON DUPLICATE KEY UPDATE date='".date("Y-m-d H:i:s")."' , results = '".$file."', virus=$virus , status=$status");
	   //$insert ="INSERT INTO clamav_logs(id, date, results, virus, status) VALUES ( '". basename($logfile)."','". date("Y-m-d H:i:s")."' ,'" .$file."',". $virus." , ".$status.") ON DUPLICATE KEY UPDATE date='".date("Y-m-d H:i:s")."' , results = '".$file."', virus=$virus , status=$status";
//	   echo "Starting Insert";
//       mysql_select_db($dbname);
//	   $result = mysql_query($insert);
//       echo "Finished Insert";
//	   if (!$result) {	die('Invalid query: ' . mysql_error());}
//       echo "Inserted";

	   if ( $status == 1 and $virus == 1 ){
           // echo "finished scan found with virus";
           // $result = mysql_query("SELECT param,val FROM misc WHERE param='clamnotify'");
            $toemail = pm_Bootstrap::getDbAdapter()->fetchOne("SELECT val FROM misc WHERE param='clamnotify'");
          
            if (!empty($toemail)) {
		          mail_attachment(basename($logfile), $Logdirectory, $toemail , $my_mail, $my_name, $my_replyto, $my_subject, $my_message);
            }
	   unlink($logfile);
        }
    }
    //mysql_close($conn);
}

function mail_attachment($filename, $path, $mailto, $from_mail, $from_name, $replyto, $subject, $message) {
    $file = $path.$filename;
    $file_size = filesize($file);
    $handle = fopen($file, "r");
    $content = fread($handle, $file_size);
    fclose($handle);
    $content = chunk_split(base64_encode($content));
    $uid = md5(uniqid(time()));
    $name = basename($file);
    $header = "From: ".$from_name." <".$from_mail.">\r\n";
    $header .= "Reply-To: ".$replyto."\r\n";
    $header .= "MIME-Version: 1.0\r\n";
    $header .= "Content-Type: multipart/mixed; boundary=\"".$uid."\"\r\n\r\n";
    $header .= "This is a multi-part message in MIME format.\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-type:text/plain; charset=iso-8859-1\r\n";
    $header .= "Content-Transfer-Encoding: 7bit\r\n\r\n";
    $header .= $message."\r\n\r\n";
    $header .= "--".$uid."\r\n";
    $header .= "Content-Type: application/octet-stream; name=\"".$filename."\"\r\n"; // use different content types here
    $header .= "Content-Transfer-Encoding: base64\r\n";
    $header .= "Content-Disposition: attachment; filename=\"".$filename."\"\r\n\r\n";
    $header .= $content."\r\n\r\n";
    $header .= "--".$uid."--";
    if (mail($mailto, $subject, "", $header)) {
    //    echo "mail send ... OK"; // or use booleans here
    } else {
    //    echo "mail send ... ERROR!";
    }
}


?>
